import ecfsConfig from '../../../config/ecfs.config.js'
import * as partyApi from '../../../api/party.js'
import { merge } from '../../../utils/util.js'
/**
 * 保存当事人，向后台发请求
 */
export function handleSaveParty(page) {
  // console.log(page.data.content)
  var that = page
  const config = {
    url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.SAVE_PARTY_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.SAVE_PARTY_URL),
    data: that.data.content,
  }
  console.log('data')
  console.log(that.data.content)
  partyApi.saveParty(config).then(res => {
    console.log(res)
    let data = res.data
    if (res.data.code === 200) {
      that.setData({
        litigationStatus: res.data.body.litigationStatus,
        litigationStatusName: res.data.body.litigationStatusName,
        partyType: res.data.body.partyType,
        partyTypeName: res.data.body.partyTypeName,
      })
      if (res.data.body.partyType === 1) {//自然人
        that.setData({
          nature: res.data.body
        });
      } else if (res.data.body.partyType === 2) {//法人
        that.setData({
          legal: res.data.body
        });
      } else if (res.data.body.partyType === 3) {//其他组织
        that.setData({
          other: res.data.body
        });
      }
      let caseInfo, flag = 0//flag判断是否在缓存中添加当前原告，0-添加
      wx.getStorage({
        key: 'caseInfo',
        success: function (res) {
          // console.log(res)
          caseInfo = res.data
        }
      })
      //保存成功返回之前界面，并将原告信息保存
      if (res.data.body.litigationStatus === 1) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 1000
        })
        //z在storage 存原告信息
        let accuser = merge({}, data.body), fileList = []//复制原告信息

        accuser.agentIds = that.data.agentIds//当事人代理人集合
        if (data.body.certificates.length > 0) {//取图片地址
          for (let index in that.data.legal.certificates) {
            let url = ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.PROVIEW_FILE_URL + data.body.certificates[index].newName) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.PROVIEW_FILE_URL + data.body.certificates[index].newName)
            fileList.push(url)
          }
        }
        accuser.fileList = fileList
        if (accuser.name.length > 10) {
          accuser.clippedName_10 = accuser.name.substring(0, 10) + '...'
        } else {
          accuser.clippedName_10 = accuser.name.substring(0, 10)
        }
        if (accuser.addressDetail.length < 3) {
          accuser.clippedAddressDetail_3 = accuser.addressDetail.substring(0, 3)
          accuser.clippedAddressDetail_10 = accuser.addressDetail.substring(0, 10)
        } else if (accuser.addressDetail.length > 10) {
          accuser.clippedAddressDetail_3 = accuser.addressDetail.substring(0, 3) + '...'
          accuser.clippedAddressDetail_10 = accuser.addressDetail.substring(0, 10) + '...'
        } else {
          accuser.clippedAddressDetail_3 = accuser.addressDetail.substring(0, 3)
          accuser.clippedAddressDetail_10 = accuser.addressDetail.substring(0, 10) + '...'
        }
        if (accuser.homeRegisterDetail.length < 3) {
          accuser.clippedHomeRegisterDetail_3 = accuser.homeRegisterDetail.substring(0, 3)
          accuser.clippedHomeRegisterDetail_10 = accuser.homeRegisterDetail.substring(0, 10)
        } else if (accuser.addressDetail.length > 10) {
          accuser.clippedHomeRegisterDetail_3 = accuser.homeRegisterDetail.substring(0, 3) + '...'
          accuser.clippedHomeRegisterDetail_10 = accuser.homeRegisterDetail.substring(0, 10) + '...'
        } else {
          accuser.clippedHomeRegisterDetail_3 = accuser.homeRegisterDetail.substring(0, 3)
          accuser.clippedHomeRegisterDetail_10 = accuser.homeRegisterDetail.substring(0, 10) + '...'
        }
        if (accuser.unitAddressDetail.length < 3) {
          accuser.clippedUnitAddressDetail_3 = accuser.unitAddressDetail.substring(0, 3)
          accuser.clippedUnitAddressDetail_10 = accuser.unitAddressDetail.substring(0, 10)
        } else if (accuser.addressDetail.length > 10) {
          accuser.clippedUnitAddressDetail_3 = accuser.unitAddressDetail.substring(0, 3) + '...'
          accuser.clippedUnitAddressDetail_10 = accuser.unitAddressDetail.substring(0, 10) + '...'
        } else {
          accuser.clippedUnitAddressDetail_3 = accuser.unitAddressDetail.substring(0, 3)
          accuser.clippedUnitAddressDetail_10 = accuser.unitAddressDetail.substring(0, 10) + '...'
        }
        if (accuser.unitName.length < 3) {
          accuser.clippedUnitName_3 = accuser.unitName.substring(0, 3)
          accuser.clippedUnitName_10 = accuser.unitName.substring(0, 10)
        } else if (accuser.addressDetail.length > 10) {
          accuser.clippedUnitName_3 = accuser.unitName.substring(0, 3) + '...'
          accuser.clippedUnitName_10 = accuser.unitName.substring(0, 10) + '...'
        } else {
          accuser.clippedUnitName_3 = accuser.unitName.substring(0, 3)
          accuser.clippedUnitName_10 = accuser.unitName.substring(0, 10) + '...'
        }
        if (accuser.gender === 1) {
          accuser.genderName = '男'
        } else {
          accuser.genderName = '女'
        }
        console.log(caseInfo)
        if (caseInfo != undefined) {
          for (let index in caseInfo.accusers) {
            if (data.body.id === caseInfo.accusers[index].partyId) {//若原告partyID存在
              flag = flag + 1
              caseInfo.accusers[index] = accuser
            }
          }
        }
        console.log('accuser')
        console.log(accuser)
        if (flag === 0) {//若之前未存该原告则push进去
          if (caseInfo != undefined) {
            caseInfo.accusers.push(accuser)
          } else {
            caseInfo = { accusers: [accuser] }
          }
        }
        wx.setStorage({//放入缓存
          key: "caseInfo",
          data: caseInfo
        })

      } else if (res.data.body.litigationStatus === 2) {//在缓存中被告信息
        let appellee = merge({}, data.body)//复制原告信息
        appellee.agentIds = that.data.agentIds
        if (data.body.certificates.length > 0) {//取图片地址
          for (let index in that.data.legal.certificates) {
            let url = ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.PROVIEW_FILE_URL + data.body.certificates[index].newName) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.PROVIEW_FILE_URL + data.body.certificates[index].newName)
            appellee.fileList.push(url)
          }
        }
        //图片地址
        if (appellee.name.length > 10) {
          appellee.clippedName_10 = appellee.name.substring(0, 10) + '...'
        } else {
          appellee.clippedName_10 = appellee.name.substring(0, 10)
        }
        if (appellee.addressDetail.length < 3) {
          appellee.clippedAddressDetail_3 = appellee.addressDetail.substring(0, 3)
          appellee.clippedAddressDetail_10 = appellee.addressDetail.substring(0, 10)
        } else if (appellee.addressDetail.length > 10) {
          appellee.clippedAddressDetail_3 = appellee.addressDetail.substring(0, 3) + '...'
          appellee.clippedAddressDetail_10 = appellee.addressDetail.substring(0, 10) + '...'
        } else {
          appellee.clippedAddressDetail_3 = appellee.addressDetail.substring(0, 3)
          appellee.clippedAddressDetail_10 = appellee.addressDetail.substring(0, 10) + '...'
        }
        if (appellee.homeRegisterDetail.length < 3) {
          appellee.clippedHomeRegisterDetail_3 = appellee.homeRegisterDetail.substring(0, 3)
          appellee.clippedHomeRegisterDetail_10 = appellee.homeRegisterDetail.substring(0, 10)
        } else if (appellee.addressDetail.length > 10) {
          appellee.clippedHomeRegisterDetail_3 = appellee.homeRegisterDetail.substring(0, 3) + '...'
          appellee.clippedHomeRegisterDetail_10 = appellee.homeRegisterDetail.substring(0, 10) + '...'
        } else {
          appellee.clippedHomeRegisterDetail_3 = appellee.homeRegisterDetail.substring(0, 3)
          appellee.clippedHomeRegisterDetail_10 = appellee.homeRegisterDetail.substring(0, 10) + '...'
        }
        if (appellee.unitAddressDetail.length < 3) {
          appellee.clippedUnitAddressDetail_3 = appellee.unitAddressDetail.substring(0, 3)
          appellee.clippedUnitAddressDetail_10 = appellee.unitAddressDetail.substring(0, 10)
        } else if (accuser.addressDetail.length > 10) {
          appellee.clippedUnitAddressDetail_3 = appellee.unitAddressDetail.substring(0, 3) + '...'
          appellee.clippedUnitAddressDetail_10 = appellee.unitAddressDetail.substring(0, 10) + '...'
        } else {
          appellee.clippedUnitAddressDetail_3 = appellee.unitAddressDetail.substring(0, 3)
          appellee.clippedUnitAddressDetail_10 = appellee.unitAddressDetail.substring(0, 10) + '...'
        }
        if (appellee.unitName.length < 3) {
          appellee.clippedUnitName_3 = appellee.unitName.substring(0, 3)
          appellee.clippedUnitName_10 = appellee.unitName.substring(0, 10)
        } else if (appellee.addressDetail.length > 10) {
          appellee.clippedUnitName_3 = appellee.unitName.substring(0, 3) + '...'
          appellee.clippedUnitName_10 = appellee.unitName.substring(0, 10) + '...'
        } else {
          appellee.clippedUnitName_3 = appellee.unitName.substring(0, 3)
          appellee.clippedUnitName_10 = appellee.unitName.substring(0, 10) + '...'
        }
        if (caseInfo.appellees != undefined) {
          for (let index in caseInfo.appellees) {
            if (res.data.body.id === caseInfo.appellees[index].partyId) {
              flag = flag + 1
              caseInfo.appellees[index] = appellee
            }
          }
        }
        if (flag === 0) {
          caseInfo.appellees.push(appellee)
        }
        wx.setStorage({
          key: "caseInfo",
          data: caseInfo
        })
      } else {//缓存第三人
        let third = merge({}, data.body)//复制原告信息
        third.agentIds = that.data.agentIds
        if (data.body.certificates.length > 0) {//取图片地址
          for (let index in that.data.legal.certificates) {
            let url = ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.PROVIEW_FILE_URL + data.body.certificates[index].newName) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.PROVIEW_FILE_URL + data.body.certificates[index].newName)
            third.fileList.push(url)
          }
        }
        if (third.name.length > 10) {
          third.clippedName_10 = third.name.substring(0, 10) + '...'
        } else {
          third.clippedName_10 = third.name.substring(0, 10)
        }
        if (third.addressDetail.length < 3) {
          third.clippedAddressDetail_3 = third.addressDetail.substring(0, 3)
          third.clippedAddressDetail_10 = third.addressDetail.substring(0, 10)
        } else if (third.addressDetail.length > 10) {
          third.clippedAddressDetail_3 = third.addressDetail.substring(0, 3) + '...'
          third.clippedAddressDetail_10 = third.addressDetail.substring(0, 10) + '...'
        } else {
          third.clippedAddressDetail_3 = third.addressDetail.substring(0, 3)
          third.clippedAddressDetail_10 = third.addressDetail.substring(0, 10) + '...'
        }
        if (third.homeRegisterDetail.length < 3) {
          third.clippedHomeRegisterDetail_3 = third.homeRegisterDetail.substring(0, 3)
          third.clippedHomeRegisterDetail_10 = third.homeRegisterDetail.substring(0, 10)
        } else if (third.addressDetail.length > 10) {
          third.clippedHomeRegisterDetail_3 = third.homeRegisterDetail.substring(0, 3) + '...'
          third.clippedHomeRegisterDetail_10 = third.homeRegisterDetail.substring(0, 10) + '...'
        } else {
          third.clippedHomeRegisterDetail_3 = third.homeRegisterDetail.substring(0, 3)
          third.clippedHomeRegisterDetail_10 = third.homeRegisterDetail.substring(0, 10) + '...'
        }
        if (third.unitAddressDetail.length < 3) {
          third.clippedUnitAddressDetail_3 = third.unitAddressDetail.substring(0, 3)
          third.clippedUnitAddressDetail_10 = third.unitAddressDetail.substring(0, 10)
        } else if (third.addressDetail.length > 10) {
          third.clippedUnitAddressDetail_3 = third.unitAddressDetail.substring(0, 3) + '...'
          third.clippedUnitAddressDetail_10 = third.unitAddressDetail.substring(0, 10) + '...'
        } else {
          third.clippedUnitAddressDetail_3 = third.unitAddressDetail.substring(0, 3)
          third.clippedUnitAddressDetail_10 = third.unitAddressDetail.substring(0, 10) + '...'
        }
        if (third.unitName.length < 3) {
          third.clippedUnitName_3 = third.unitName.substring(0, 3)
          third.clippedUnitName_10 = third.unitName.substring(0, 10)
        } else if (third.addressDetail.length > 10) {
          third.clippedUnitName_3 = third.unitName.substring(0, 3) + '...'
          third.clippedUnitName_10 = third.unitName.substring(0, 10) + '...'
        } else {
          third.clippedUnitName_3 = third.unitName.substring(0, 3)
          third.clippedUnitName_10 = third.unitName.substring(0, 10) + '...'
        }
        if (caseInfo.thirds != undefined) {
          for (let index in caseInfo.thirds) {
            if (res.data.body.id === caseInfo.thirds[index].partyId) {
              flag = flag + 1
              caseInfo.thirds[index] = third
            }
          }
        }
        if (flag === 0) {
          caseInfo.thirds.push(third)
        }
        wx.setStorage({//放入缓存
          key: "caseInfo",
          data: caseInfo
        })
      }
      wx.setStorage({
        key: "caseId",
        data: res.data.body.caseId
      })
    } else if (res.data.code === 204003) {//请勿重复添加
      if (res.data.body.partyType === 1) {
        that.showTips('该当事人的身份证号已经存在，请勿重复添加')
      } else {
        wx.showToast({
          title: '该组织机构代码已经存在，请勿重复添加',
          icon: 'none',
          duration: 1000
        })
      }
    } else if (res.data.code === 204010) {
      if (res.data.body.litigationStatus === 2) {
        wx.showToast({
          title: '当前认证用户不可作为被告',
          icon: 'none',
          duration: 1000
        })
      } else {
        wx.showToast({
          title: '当前认证用户不可作为第三人',
          icon: 'none',
          duration: 1000
        })
      }
    } else {
      console.log('save')
      console.log(res.data)
      wx.showToast({
        title: '该组织机构代码已经存在，请勿重复添加',
        icon: 'none',
        duration: 1000
      })
    }
  }).catch(error => {
    console.log(error)
  })
}
/**
 * 删除当事人向后台发请求
 */
export function handleDeleteParty(page, deleteContent) {
  var that = page
  const config = {
    url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.DELETE_PARTY_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.DELETE_PARTY_URL),
    data: deleteContent,
  }
  partyApi.deleteParty(config).then(res => {
    if (res.data.code === 200) {
      //删除成功返回之前界面，并将原告信息删除
      wx.showToast({
        title: '删除成功',
        icon: 'success',
        duration: 1000
      })
    } else {
      wx.showToast({
        title: '删除失败',
        icon: 'success',
        duration: 1000
      })
      console.log(res)
    }
  }).catch(error => {
    console.log(error)
  })
}